package com.xiaodong.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.entity.Employee;
import com.xiaodong.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //用户登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //将密码进行MD5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //查询数据库里面里的该数据
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        //判断数里不存在该数据
        if (one == null) {
            return R.error("登录失败");
        }

        //判断数里存在该数据 进行密码比对
        if (!one.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //查看用户的状态
        if (one.getStatus() == 0) {
            return R.error("用户已禁用");
        }

        //登录成功
        request.getSession().setAttribute("employee", one.getId());

        return R.success(one);
    }

    //用户退出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {

        request.getSession().removeAttribute("employee");
        return R.success("退出成功");

    }

    //查询员工信息
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

//        log.info("page={}  pageSize={}  name=()",page,pageSize,name);

        Page pageInfo = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        if(name!=null){

            queryWrapper.like(Employee::getName,name);

        }
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    //启用禁用修改
    @PutMapping
    public R<String> update(@RequestBody Employee employee){

//        Long id =(Long) request.getSession().getAttribute("employee");

//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(id);
        employeeService.updateById(employee);
        return R.success("该员工账号已禁用成功");

    }

    //数据回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){

        Employee byId = employeeService.getById(id);

        if(byId!=null){
            return R.success(byId);
        }

        return R.error("没有查询到对应员工信息");
    }

    //新增员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {

        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);

        return R.success("员工已添加成功");

    }

}
